package com.srs.action.role;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import com.srs.action.BaseAction;
import com.srs.dao.RGroupRoleDao;
import com.srs.dao.cfg.RoleDao;
import com.srs.model.cfg.Role;

public class RoleAction extends BaseAction {

	private Integer page;
	private Integer pageNum;	
	private Integer start;
	private Integer end;
	private Integer total;	
	
	private Role role;
	private List<Role> roles;

	private String rolesInGroup;
	
	private String json;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Action role/list - Role management
	 * 
	 * @return
	 */
	public String list() {
		System.out.println("RoleAction::list");
		RoleDao roleDao = new RoleDao();
		roles = roleDao.list();
		
		if (page==null){
			page = 1;
		}
		if (roles.size()%RECORD_PER_PAGE==0){
			pageNum = roles.size()/RECORD_PER_PAGE;
		} else {
			pageNum = roles.size()/RECORD_PER_PAGE + 1;
		}
		
		start = (page-1)*RECORD_PER_PAGE + 1;
		total = roles.size();
		if (total<(page)*RECORD_PER_PAGE){
			end = total;
		} else {
			end = (page)*RECORD_PER_PAGE;
		}
		
		List<Role> pageRoles = new ArrayList<Role>() ;
		for(int i=0; i<roles.size(); i++){
			if (i>=(page-1)*RECORD_PER_PAGE && i<(page)*RECORD_PER_PAGE){
				roles.get(i).setIndex(i+1);
				pageRoles.add(roles.get(i));
			}
		}
		roles = pageRoles;		
		return SUCCESS;
	}

	/**
	 * Action role/view - Role management
	 * 
	 * @return
	 */
	public String view() {
		System.out.println("RoleAction::view");
		RoleDao roleDao = new RoleDao();
		role = roleDao.get(role.getRoleId());
		return SUCCESS;
	}

	/**
	 * Action role/add - Role management
	 * 
	 * @return
	 */
	public String add() {
		System.out.println("RoleAction::add");
		return SUCCESS;
	}

	/**
	 * Action role/addComplete - Role management
	 * 
	 * @return
	 */
	public String addComplete() {
		System.out.println("RoleAction::addComplete");
		RoleDao roleDao = new RoleDao();
		roleDao.insert(role);		
		return SUCCESS;
	}

	/**
	 * Action role/edit - Role management
	 * 
	 * @return
	 */
	public String edit() {
		System.out.println("RoleAction::edit");
		RoleDao roleDao = new RoleDao();
		role = roleDao.get(role.getRoleId());
		return SUCCESS;
	}

	/**
	 * Action role/editComplete - Role management
	 * 
	 * @return
	 */
	public String editComplete() {
		System.out.println("RoleAction::editComplete");
		RoleDao roleDao = new RoleDao();
		roleDao.update(role);
		return SUCCESS;
	}

	/**
	 * Action role/deleteComplete - Role management
	 * 
	 */
	public String deleteComplete() {
		System.out.println("RoleAction::deleteComplete");
		RoleDao roleDao = new RoleDao();
		roleDao.delete(role.getRoleId());
		return SUCCESS;
	}

	/**
	 * Action group/listJson - Role management
	 * 
	 */
	public String nameIsUsed() {
		System.out.println("RoleAction::listJson");

		// Init Dao
		RoleDao roleDao = new RoleDao();
		
		// Put to json;
		setJson("{nameIsUsed: " +
				JSONArray.fromObject(roleDao.nameIsUsed(role.getRoleName())).toString() +
				"}");
		return SUCCESS;
	}	
	
	public String roleIsUsed() {
		System.out.println("RoleAction::listJson");

		// Init Dao
		RGroupRoleDao groupRoleDao = new RGroupRoleDao();
		
		// Put to json;
		setJson("{roleIsUsed: " +
				JSONArray.fromObject(groupRoleDao.roleIsUsed(role.getRoleId())).toString() +
				"}");
		return SUCCESS;		
	}
	
	/**
	 * Action role/listJson - Role management
	 * 
	 */
	public String listJson() {
		System.out.println("RoleAction::listJson");

		// Init Dao
		RoleDao roleDao = new RoleDao();
		
		
		// Put to json;
		setJson("{roles: " +
				JSONArray.fromObject(roleDao.list(rolesInGroup)).toString() +
				"}");
		return SUCCESS;
	}	
	
	/**
	 * @return the page
	 */
	public Integer getPage() {
		return page;
	}

	/**
	 * @param page the page to set
	 */
	public void setPage(Integer page) {
		this.page = page;
	}

	/**
	 * @return the pageNum
	 */
	public Integer getPageNum() {
		return pageNum;
	}

	/**
	 * @param pageNum the pageNum to set
	 */
	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	/**
	 * @return the start
	 */
	public Integer getStart() {
		return start;
	}

	/**
	 * @param start the start to set
	 */
	public void setStart(Integer start) {
		this.start = start;
	}

	/**
	 * @return the end
	 */
	public Integer getEnd() {
		return end;
	}

	/**
	 * @param end the end to set
	 */
	public void setEnd(Integer end) {
		this.end = end;
	}

	/**
	 * @return the total
	 */
	public Integer getTotal() {
		return total;
	}

	/**
	 * @param total the total to set
	 */
	public void setTotal(Integer total) {
		this.total = total;
	}

	/**
	 * @return the role
	 */
	public Role getRole() {
		return role;
	}

	/**
	 * @param role the role to set
	 */
	public void setRole(Role role) {
		this.role = role;
	}

	/**
	 * @return the roles
	 */
	public List<Role> getRoles() {
		return roles;
	}

	/**
	 * @param roles the roles to set
	 */
	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	/**
	 * @return the rolesInGroup
	 */
	public String getRolesInGroup() {
		return rolesInGroup;
	}

	/**
	 * @param rolesInGroup the rolesInGroup to set
	 */
	public void setRolesInGroup(String rolesInGroup) {
		this.rolesInGroup = rolesInGroup;
	}

	/**
	 * @return the json
	 */
	public String getJson() {
		return json;
	}

	/**
	 * @param json the json to set
	 */
	public void setJson(String json) {
		this.json = json;
	}
	
}
